//
//  MASExampleTouchMoveView.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/12.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "MASExampleTouchMoveView.h"

@interface MASExampleTouchMoveView()
@property (nonatomic, strong) MASConstraint *leftConstraint; // 保存左边的约束，用于在移动时调整位置
@property (nonatomic, strong) MASConstraint *topConstraint; // 保存顶部的约束，用于在移动时调整位置
@end

@implementation MASExampleTouchMoveView
- (id)init {
    self = [super init];
    if (!self) return nil;
    UILabel *label = [LayoutUtils fixedLabelWithText:@"Move Me"];
    [self addSubview:label];
    [label makeConstraints:^(MASConstraintMaker *make) {
        self.leftConstraint = make.centerX.equalTo(self.mas_left).with.offset(50).priorityHigh();
        self.topConstraint = make.centerY.equalTo(self.mas_top).with.offset(50).priorityHigh();
        
        // 边界条件约束，保证内容可见，优先级1000
        make.left.greaterThanOrEqualTo(kPadding);
        make.right.lessThanOrEqualTo(kPadding);
        make.top.greaterThanOrEqualTo(kPadding);
        make.bottom.lessThanOrEqualTo(kPadding);
    }];
    return self;
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    NSLog(@"%@",NSStringFromCGPoint(touchLocation));
    self.leftConstraint.offset = touchLocation.x;
        self.topConstraint.offset = touchLocation.y;
}
@end
